/*
 MIT License
 
 Copyright (c) 2021 Steven Le

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

/**
 *	lx_spwd.c - version 0.1
 *	
 *	starts in current directory and recursively
 *	climbs up to root of filesystem, prints top part
 *	then prints current part
 *      
 *	use readdir() to get info about each thing      
 *      
 *      bug: prints an empty string if run from "/"
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

inot_t get_inode(char*);
void printpathto(ino_t);
int inum_to_name(ino_t, char *, int);

int main()
{
	printpathto(get_inode("."));
	putchar('\n');			/* print each to here */
	return 0;
}

/**
 *	prints each leading down to an object with this node.
 *　	kindof recursive
 */
void printpathto(ino_t this_node)
{
	ino_t my_inode;
	char its_name[BUFSIZ];
	if(get_inode("..") != this_inode) {
		chdir("..");					/* up one dir  */
		inum_to_name(this_inode, its_name, BUFSIZ);	/* get its name*/
		my_inode = get_inode(".");			/* print head  */
		printpathto(my_inode);				/* recursively  */
		printf("/%s", its_name);			/* name of birth */
	}
}

/**
 *	looks through current directory for a file with this node
 *　	number and copies its name into namebuf
 */
int inum_to_name(ino_t, char *, int)
{
	DIR 	*dir_ptr;					/* the directory  */
	struct  dirent = direntp;				/* each entry */
	dir_ptr = opendir(".");					
	if (dir_ptr == NULL)　{
		perror(".");
		exit(1);
	}

	/*
 	 *	search directory for a file with specified inum
	 */ 
	while((direntp = readdir(dir_ptr)) != NULL)
	{
		if (direntp->d_ino == inode_to_find)
		{
			strncpy(namebuf, direntp->name, buflen);
			namebuf[buflen - 1] = '\0'; 		/* just in case  */
			closedir(dir_ptr);
			return;
		}
	}
	fprintf(stderr, "error looking for inum %d\n", inode_to_find);
	exit(1);
}

/*
 *	returns inode number of the file
 */
ino_t get_inode(char *fname)
{
	struct stat info;
	if(stat(fname, &info) == -1 )
	{
		printf(stderr, "Cannot stat");
		perror(fname);
		exit(1);
	}
	return info.st_ino;	
}

